/**
 *
 */
package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;

/**
 * Helper class for the properties.
 * 
 * @author Marco
 */
public class ApplicationProperties {
    public static final String DEFAULT_CHARSET = "default";

    private static Properties  appProperties;
    private static File        propertyFile;

    static {
        try {
            appProperties = new Properties();
            propertyFile = new File("config/application_settings.properties");
            FileInputStream in = new FileInputStream(propertyFile);
            appProperties.load(in);
            in.close();

        }
        catch (FileNotFoundException anEx) {
            anEx.printStackTrace();
        }
        catch (IOException anEx) {
            anEx.printStackTrace();
        }
    }

    /**
     * Returns the value to a key or {@code null} if the property wasn't found.
     * 
     * @param aKey
     *            they key to get the value from
     * @return the value to the key
     */
    public static String get(String aKey) {
        return appProperties.getProperty(aKey);
    }

    public void set(String aValue) {
        FileOutputStream out;
        try {
            out = new FileOutputStream(propertyFile);
            appProperties.store(out, "");
        }
        catch (FileNotFoundException anEx) {
            anEx.printStackTrace();
        }
        catch (IOException anEx) {
            anEx.printStackTrace();
        }
    }

    /**
     * Returns the defined charset name. Is "default" defined, the OS default
     * charset name is returned.
     * 
     * You can still use the {@link #get(String)} with "charset", but use this
     * method to be sure with the default charset.
     * 
     * @return the defined charset name
     */
    public static String getCharset() {
        if (appProperties.getProperty("charset").equalsIgnoreCase(
                DEFAULT_CHARSET)) {
            return Charset.defaultCharset().name();
        }
        return appProperties.getProperty("charset");
    }

    /**
     * Returns the root folder for series containing the video files.
     * 
     * You can still use the {@link #get(String)} with
     * "root_directory_for_series", but this is more comfortable since it
     * returns an File object.
     * 
     * @return root directory for series
     */
    public static File getSeriesRoot() {
        return new File(appProperties.getProperty("root_directory_for_series"));
    }

    /**
     * Returns the root folder for the episode title files.
     * 
     * You can still use the {@link #get(String)} with
     * "root_directory_for_episode_titles", but this is more comfortable since
     * it returns an File object.
     * 
     * @return root directory for episode titles
     */
    public static File getEpisodetitlesRoot() {
        return new File(
                appProperties.getProperty("root_directory_for_episode_titles"));
    }

    /**
     * Returns the root folder for the episode title files.
     * 
     * You can still use the {@link #get(String)} with "season_label", but this
     * is more comfortable.
     * 
     * @return root the label for seasons
     */
    public static String getSeasonLabel() {
        return appProperties.getProperty("season_label");
    }

    /**
     * Returns the root folder for the episode title files.
     * 
     * You can still use the {@link #get(String)} with
     * "allowed_filename_extensions", but this already return it as String[] and
     * trims each filename extensions if the users was to stupid to read the
     * property comments.
     * 
     * @return root the label for seasons
     */
    public static String[] getAllowedFilenameExtensions() {
        String[] tempAllowed = appProperties.getProperty(
                "allowed_filename_extensions").split(",");
        for (int tempI = 0; tempI < tempAllowed.length; tempI++) {
            tempAllowed[tempI] = tempAllowed[tempI].trim();
        }

        return tempAllowed;
    }

    /**
     * Get the file where this properties were loaded from
     * 
     * @return the property file
     */
    public static File getPropertyFile() {
        return propertyFile;
    }

    // TODO method for filename format which returns a filename formatter or
    // something

}
